﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.DTO;
using mendian;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.辅料
{
    public partial class Frm款式类别下新增辅料类别 : DevExpress.XtraEditors.XtraForm
    {
        public List<DTO辅料大分类> dto辅料大分类 = new List<DTO辅料大分类>();
        public List<DTO辅料大分类> dTOs = new List<DTO辅料大分类>();
        public String id;
        public String name;
        public String code;
        public Frm款式类别下新增辅料类别(List<DTO辅料大分类> dto辅料大分类)
        {
            this.dto辅料大分类 = dto辅料大分类;
            InitializeComponent();
            this.getallBaseMaterialCatelog();
        }
        #region 选择辅料分类
        /// <summary>
        /// 绑定清空事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit辅料分类_Popup(object sender, EventArgs e)
        {
            //得到当前SearchLookUpEdit弹出窗体
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn辅料分类_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        /// <summary>
        /// 点击选择后的行为
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit辅料分类View_Click(object sender, EventArgs e)
        {
            var a = this.searchLookUpEdit辅料分类.Properties.View.GetSelectedRows();
            foreach (int rowHandle in a)
            {
                this.id = this.searchLookUpEdit辅料分类.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                this.name = this.searchLookUpEdit辅料分类.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                this.code = this.searchLookUpEdit辅料分类.Properties.View.GetRowCellValue(rowHandle, "code").ToString();
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn辅料分类_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit辅料分类.ToolTip = null;
            searchLookUpEdit辅料分类.EditValue = null;
        }
        /// <summary>
        /// 选完之后的显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit辅料分类_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.code+this.name;
        }

        #endregion

        private void simpleButton保存_Click(object sender, EventArgs e)
        {
            foreach(DTO辅料大分类 dTO in this.dto辅料大分类)
            {
                if (dTO.id.Equals(this.id))
                {
                    MessageBox.Show("重复添加");
                    return;
                }
            }
            this.dto辅料大分类.Add(new DTO辅料大分类(id, name, code));
            this.Close();
        }

        private void simpleButton取消_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void getallBaseMaterialCatelog()
        {
            Dictionary<String, String> dic = new Dictionary<string, string>();
            dic = FunctionHelper.JsonDeserialization<Dictionary<String, String>>(RestCall.httpGetMethod(URL.接口url+"/acc/getBaseMaterialCatelog"));
            foreach(KeyValuePair<String,String> kvp in dic)
            {
                this.dTOs.Add(new DTO辅料大分类(kvp.Key, kvp.Value, ""));
            }
            this.searchLookUpEdit辅料分类.Properties.DataSource = this.dTOs;
        }
    }
}
